<?php
namespace app\modules\app\models;
use app\libs\Method;
use app\modules\cn\models\User;
use yii\db\ActiveRecord;
use yii;

class IntegralBuy extends ActiveRecord
{
    public static function tableName()
    {
        return '{{%integral_buy}}';
    }

    /**
     * 是否购买
     * @param $uid
     * @param $id
     * @param $type
     * @return int
     */
    public static function isBuy($uid=0,$id,$type){
        if(!$uid) return 0;
        $data = IntegralBuy::find()->where("uid=$uid and contentId=$id and type=$type")->one();
        return $data?1:0;
    }

    /**
     * 课程视频已购买
     * @param $uid
     * @param $contentId
     * @return array
     */
    public static function videoIsBuy($uid,$contentId){
        $data = IntegralBuy::find()
            ->select('g.goodsId')
            ->from('{{%integral_buy}} b')
            ->leftJoin('{{%integral_goods}} g','b.id=g.orderId')
            ->where("b.type=1 and b.contentId=$contentId and g.uid=$uid")
            ->asArray()
            ->all();
        if($data){
            $ids = array_filter(array_column($data,'goodsId'));
        }else{
            $ids = [];
        }
        return $ids;
    }

    /**
     * 课程视频已购买-单个
     * @param $uid
     * @param $id
     * @param $type
     * @return int
     */
    public static function videoIsBuyOne($uid,$id,$type){
        $data = IntegralBuy::find()
            ->select('g.goodsId')
            ->from('{{%integral_buy}} b')
            ->leftJoin('{{%integral_goods}} g','b.id=g.orderId')
            ->where("b.type=$type and g.goodsId=$id and g.uid=$uid")
            ->asArray()
            ->one();
        return $data?1:0;
    }

    public static function goodsBuy($uid=0,$info,$data){
        $time = time();
        $model = new IntegralBuy();
        $model->uid = $uid;
        $model->orderNumber = Method::orderNumber();
        $model->type = $info['type'];
        $model->contentId = $info['contentId'];
        $model->integral = $info['integral'];
        $model->isAll = $info['isAll'];
        $model->name = $info['name'];
        $model->phone = $info['phone'];
        $model->createTime = $time;
        $res = $model->save();
        if($res){
            $addId = $model->primaryKey;
            foreach ($data as $k=>$v){
                $goods = new IntegralGoods();
                $goods->orderId = $addId;
                $goods->uid = $uid;
                $goods->goodsId = $v['id'];
                $goods->goodsName = $v['title'];
                $goods->sdk = $v['sdk'];
                $goods->createTime = $time;
                $goods->save();
            }
            if($info['type']==1){
                $msg = '免费课程视频';
            }elseif($info['type']==2){
                $msg = '公开课视频';
            }else{
                $msg = '其它';
            }
            if($info['integral']>0){
                uc_user_edit_integral3($uid,$msg,2,$info['integral'],'雅思APP');
            }
        }
    }

    //蜜糖记录
    public static function record($uid,$type,$page=1,$pageSize=10){
        if($type==1){
            $offset = ($page-1)*$pageSize;
            $data = IntegralBuy::find()
                ->select('b.id,b.orderNumber,b.contentId,b.type,b.isAll,b.createTime,g.goodsName')
                ->from('{{%integral_buy}} b')
                ->leftJoin('{{%integral_goods}} g','b.id=g.orderId')
                ->where("b.uid=$uid and b.type in (1,2)")
                ->groupBy('b.id')
                ->orderBy('id desc')
                ->offset($offset)
                ->limit($pageSize)
                ->asArray()
                ->all();
            foreach ($data as $k=>$v){
                $data[$k]['createTime'] = date('Y-m-d H:i',$v['createTime']);
                $data[$k]['goodsName'] = $v['isAll']?'【全】'.$v['goodsName']:$v['goodsName'];
            }
        }else{
            //蜜糖充值
            $pay = Method::curl_post_fix(Yii::$app->params['thinkuOrderUrl'].'/pay/api/order-list',['where'=>"uo.uid=$uid and uo.type=2 and uo.status=3",'pageSize'=>$pageSize,'page'=>$page]);
            $pay = json_decode($pay,true)['data'];
            $data = [];
            foreach ($pay as $k=>$v){
                $data[$k]['orderNumber'] = $v['orderNumber'];
                $data[$k]['contentId'] = '';
                $data[$k]['type'] = 99;
                $data[$k]['createTime'] = date('Y-m-d H:i',$v['createTime']);
                $data[$k]['num'] = $v['totalMoney']*100;
            }
        }
        return $data;
    }
}